<?php
//session_name(DicomScience);
//session_start();

/*
 * The readReports.php Controller listens on the following hhtp_get options:
 * - "lastdays" an integer for last_modified
 * - "case" an integer representing the case ID, returns the full case details of one case
 * - "search" a string searchable in all case fields
 * - "editor" an integer representing the author / editor of the case
 * The controller always responds in an XML coded http - Datastream. 
 */

$lastDays = $_GET['lastdays'];
$caseId = $_GET['case'];
$editor = $_GET['editor'];
$searchString = $_GET['search'];

$now = time();
$oneday = 86400;
$range = $lastdays * $oneday;
$lastdate = $now - $range;

/*
 * ACTION TRIGGER: which response is appropriate?
 */
if (! $lastDays == ""){
	# See Section GETCASELIST
	getCaseList($lastDays);
} elseif (! $caseId == ""){
	# See Section GETCASEBYID
	getCaseByNumber($caseId);
} elseif (! $searchString == ""){
	# See Section SEARCHREPORT
	getCaseBySearch($searchString);
} elseif (! $editor == ""){
	# See Section GETCASEBYEDITOR
	getCaseByEditor($editor);
}

/*
 * SECTION GETCASELIST (LASTDAYS)
 */
function getCaseList($lastdays){

	$now = time();
	$oneday = 86400;
	$range = $lastdays * $oneday;
	$lastdate = $now - $range;	
	
	require('../includes/gset.php');
	include ('../includes/database/connect.php');
	$sql_getCase = "SELECT cases.id_case, cases.editor, cases.last_modified, cases.topic, cases.status, staff.firstname, staff.lastname FROM cases, staff WHERE status > 4 AND cases.editor = staff.id_staff AND cases.last_modified > $lastdate ORDER BY (last_modified) DESC";	
	$getCase = mysql_query($sql_getCase);
	include ('../includes/database/closedb.php');
	
	// Writing XML Response Header
	header("Content-type: text/xml"); 
	echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
	echo "<caseList>";
	
	while ($case = mysql_fetch_object($getCase)){
		$idcase = $case->id_case;
		$lastname = $case->lastname;
		$firstname = $case->firstname;
		$topic = $case->topic;
		$status = $case->status;
		$editor = $case->editor;
		$lastmodified = $case->last_modified;
		
		$date = getDisplayDate($lastmodified);
		
		echo "<case>";
		echo "<caseId>";
		echo $idcase;
		echo "</caseId>";
		echo "<topic>";
		echo $topic;
		echo "</topic>";
		echo "<status>";
		echo $status;
		echo "</status>";
		echo "<editor>";
		echo $firstname." ".$lastname;
		echo "</editor>";
		echo "<caseEditorId>";
		echo $editor;
		echo "</caseEditorId>";
		echo "<lastModified>";
		echo $date;
		echo "</lastModified>";
		echo "</case>";
		
	}
	
	echo "</caseList>";
	
}


/*
 * SECTION GETCASEBYID
 */
function getCaseByNumber($daCase){
	
	$now = time();
	$oneday = 86400;
	$range = $lastdays * $oneday;
	$lastdate = $now - $range;	
	
	require('../includes/gset.php');
	include ('../includes/database/connect.php');
	$sql_getCase = "SELECT cases.id_case, cases.topic, cases.status, cases.editor, cases.last_modified, staff.firstname, staff.lastname FROM cases, staff WHERE cases.id_case = $daCase GROUP BY (id_case)";
	$getCase = mysql_query($sql_getCase);
	include ('../includes/database/closedb.php');
	
	// Writing XML Response Header
	header("Content-type: text/xml"); 
	echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
	echo "<caseList>";
	
	while ($case = mysql_fetch_object($getCase)){
		$idcase = $case->id_case;
		$ceditor = $case->editor;
		$lastname = $case->lastname;
		$firstname = $case->firstname;
		$topic = $case->topic;
		$status = $case->status;
		$lastmodified = $case->last_modified;
		
		$date = getDisplayDate($lastmodified);
	
		echo "<case>";
			echo "<caseId>";
			echo $idcase;
			echo "</caseId>";
			echo "<topic>";
			echo $topic;
			echo "</topic>";
			echo "<caseStatus>";
			echo $status;
			echo "</caseStatus>";
			echo "<caseEditor>";
			echo $firstname." ".$lastname;
			echo "</caseEditor>";
			echo "<caseEditorId>";
			echo $ceditor;
			echo "</caseEditorId>";
			echo "<caseLastModified>";
			echo $date;
			echo "</caseLastModified>";
			
			echo "<reportList>";
			include ('../includes/database/connect.php');
			$sql_getReport = "SELECT reports.id_report, reports.headline, reports.diagnosis, reports.report, reports.last_modified, reports.id_staff, reports.status, staff.firstname, staff.lastname FROM reports, staff, cases, creps WHERE (cases.id_case = $daCase AND reports.id_staff = staff.id_staff AND creps.id_case = cases.id_case AND creps.id_report = reports.id_report) GROUP BY id_report";
			$getReport = mysql_query($sql_getReport);
			include ('../includes/database/closedb.php');
			
			while ($report = mysql_fetch_object($getReport)){
				$idreport = $report->id_report;
				$headline = $report->headline;
				$diagnosis = $report->diagnosis;
				$reportText = $report->report;
				$lastmodified = $report->last_modified;
				$id_staff = $report->id_staff;
				$rstatus = $report->status;
				$firstname = $report->firstname;
				$lastname = $report->lastname;
				$status = $report->status;
				
				$date = getDisplayDate($lastmodified);
				echo "<report>";					
					echo "<reportId>";
					echo $idreport;
					echo "</reportId>";
					echo "<headline>";
					echo $headline;
					echo "</headline>";
					echo "<diagnosis>";
					echo $diagnosis;
					echo "</diagnosis>";
					echo "<reportText>";
					echo $reportText;
					echo "</reportText>";
					echo "<reportEditor>";
					echo $firstname." ".$lastname;
					echo "</reportEditor>";
					echo "<reportEditorId>";
					echo $id_staff;
					echo "</reportEditorId>";
					echo "<reportLastModified>";
					echo $date;
					echo "</reportLastModified>";
					echo "<reportStatus>";
					echo $status;
					echo "</reportStatus>";
				echo "</report>";
			}
			
			echo "</reportList>";
			echo "<mediaList>";
			
			include ('../includes/database/connect.php');
			$sql_getMedia = "SELECT media.id_media, media.study_instance_uid, media.series_instance_uid, media.tags, media.modality, media.organ, media.location_thumb, media.location_media, media.medrep, media.imgrep, media.id_staff, media.upload_date, media.upload_user, media.series_number, media.pat_id, media.media_type, staff.firstname, staff.lastname FROM media, staff, c_media, cases WHERE (cases.id_case = $daCase) AND (cases.id_case = c_media.id_case) AND (c_media.id_media = media.id_media) AND (staff.id_staff = cases.editor) AND (c_media.media_type != '8')";
			$sql_getImages = "SELECT image.id_image, image.tags, image.modality, image.organ, image.location_thumb, image.location_image, image.window_center, image.window_width, image.laterality, image.image_comment, image.contrast, image.mr_aquisitiontype, image.sequence_name, image.slice_thickness, image.kv, image.protocol_name, image.kernel, image.mr_coil, image.flip_angle, image.pat_pos, image.sens, image.manufacturer, image.upload_date, image.date, image.upload_user, staff.lastname, staff.firstname, c_media.media_type FROM image, c_media, cases, staff WHERE (cases.id_case = $daCase) AND (cases.id_case = c_media.id_case) AND (c_media.id_media = image.id_image) AND (staff.id_staff = cases.editor) AND (c_media.media_type = '8')";
			$getMedia = mysql_query($sql_getMedia);
			$getImages = mysql_query($sql_getImages);
			include ('../includes/database/closedb.php');
			
			while ($mList = mysql_fetch_object($getMedia)){
				
				$id_media = $mList->id_media;
				$tags = $mList->tags;
				$modality = $mList->modality;
				$organ = $mList->organ;
				$location_thumb = $mList->location_thumb;
				$location_media = $mList->location_media;
				$upload_date = $mList->upload_date;
				$upload_user = $mList->upload_user;
				$media_type = $mList->media_type; 
				$firstname = $mList->firstname;
				$lastname = $mList->lastname;
				
				$date = getDisplayDate($lastmodified);
				
				echo "<media>";
					echo "<mediaId>";
					echo $id_media;
					echo "</mediaId>";
					echo "<mediaType>";
					echo $media_type;
					echo "</mediaType>";
					echo "<tags>";
					echo $tags;
					echo "</tags>";
					echo "<modality>";
					echo $modality;
					echo "</modality>";
					echo "<organ>";
					echo $organ;
					echo "</organ>";
					echo "<thumbnail>";
					echo $location_thumb;
					echo "</thumbnail>";
					echo "<mediaLocator>";
					echo $location_media;
					echo "</mediaLocator>";
					echo "<mediaEditor>";
					echo $firstname." ".$lastname;
					echo "</mediaEditor>";
					echo "<uploadDate>";
					echo $date;
					echo "</uploadDate>";
					echo "<windowCenter></windowCenter>";
                    echo "<windowWidth></windowWidth>";                                  
                    echo "<laterality></laterality>";
                    echo "<imageComment></imageComment>";
                    echo "<contrast></contrast>";
                    echo "<mrAquisitionType></mrAquisitionType>";
                    echo "<sequenceName></sequenceName>";
                    echo "<sliceThickness></sliceThickness>";
                    echo "<kV></kV>";
                    echo "<protocolName></protocolName>";
                    echo "<kernel></kernel>";
                    echo "<mrCoil></mrCoil>";
                   	echo "<flipAngle></flipAngle>";
                    echo "<patPos></patPos>";
                   	echo "<sens></sens>";
                    echo "<manufacturer></manufacturer>";
				echo "</media>";
				
			}
			
			while($iList = mysql_fetch_object($getImages)){
				
				$id_image = $iList->id_image;
				$media_type = $iList->media_type;
				$tags = $iList->tags;
				$modality = $iList->modality;
				$organ = $iList->organ;
				$thumbnail = $iList->location_thumb;
				$mediaLocator = $iList->location_image;
				$lastname = $iList->lastname;
				$firstname = $iList->firstname;
				$upload_date = $iList->upload_date;
				$windowCenter = $iList->window_center;
				$windowWidth = $iList->window_width;
				$lat = $iList->laterality;
				$imageComment = $iList->image_comment;
				$contrast = $iList->contrast;
				$mrAquistitionType = $iList->mr_aquisitiontype;
				$sequenceName = $iList->sequence_name;
				$sliceThickness = $iList->slice_thickness;
				$kv = $iList->kv;
				$protocolName = $iList->protocol_name;
				$kernel = $iList->kernel;
				$mrCoil = $iList->mr_coil;
				$flipAngle = $iList->flip_angle;
				$patPos = $iList->pat_pos;
				$sens = $iList->sens;
				$man = $iList->manufacturer;
				
				$date = getDisplayDate($upload_date);
				
				echo "<media>";
					echo "<mediaId>";
					echo $id_image;
					echo "</mediaId>";
					echo "<mediaType>";
					echo $media_type;
					echo "</mediaType>";
					echo "<tags>";
					echo $tags;
					echo "</tags>";
					echo "<modality>";
					echo $modality;
					echo "</modality>";
					echo "<organ>";
					echo $organ;
					echo "</organ>";
					echo "<thumbnail>";
					echo $thumbnail;
					echo "</thumbnail>";
					echo "<mediaLocator>";
					echo $mediaLocator;
					echo "</mediaLocator>";
					echo "<mediaEditor>";
					echo $firstname." ".$lastname;
					echo "</mediaEditor>";
					echo "<uploadDate>";
					echo $date;
					echo "</uploadDate>";
					echo "<windowCenter>";
					echo $windowCenter;
					echo "</windowCenter>";
					echo "<windowWidth>";
					echo $windowWidth;
					echo "</windowWidth>";
					echo "<laterality>";
					echo $lat;
					echo "</laterality>";
					echo "<imageComment>";
					echo $imageComment;
					echo "</imageComment>";
					echo "<contrast>";
					echo $contrast;
					echo "</contrast>";
					echo "<mrAquisitionType>";
					echo $mrAquistitionType;
					echo "</mrAquisitionType>";
					echo "<sequenceName>";
					echo $sequenceName;
					echo "</sequenceName>";
					echo "<sliceThickness>";
					echo $sliceThickness;
					echo "</sliceThickness>";
					echo "<kV>";
					echo $kv;
					echo "</kV>";
					echo "<protocolName>";
					echo $protocolName;
					echo "</protocolName>";
					echo "<kernel>";
					echo $kernel;
					echo "</kernel>";
					echo "<mrCoil>";
					echo $mrCoil;
					echo "</mrCoil>";
					echo "<flipAngle>";
					echo $flipAngle;
					echo "</flipAngle>";
					echo "<patPos>";
					echo $patPos;
					echo "</patPos>";
					echo "<sens>";
					echo $sens;
					echo "</sens>";
					echo "<manufacturer>";
					echo $man;
					echo "</manufacturer>";
					
				echo "</media>";
			}
			
			echo "</mediaList>";
			
			echo "<discussionList>";
			
			include ('../includes/database/connect.php');
			$sql_getComments = "SELECT comments.id_comment, comments.comment, comments.posted, staff.firstname, staff.lastname FROM cases, staff, comments WHERE cases.id_case = $daCase AND comments.id_case = cases.id_case AND staff.id_staff = comments.id_staff ORDER BY (id_comment) DESC";
			$getComments = mysql_query($sql_getComments);
			include ('../includes/database/closedb.php');		
			
			while ($com = mysql_fetch_object($getComments)){
				
				$firstname = $com->firstname;
				$lastname = $com->lastname;
				$id_comment = $com->id_comment;
				$comment = $com->comment;
				$lastmodified = $com->posted;
				
				$date = getDisplayDate($lastmodified);
				
				echo "<comment>";
					echo "<commentEditor>";
					echo $firstname." ".$lastname;
					echo "</commentEditor>";
					echo "<commentText>";
					echo $comment;
					echo "</commentText>";
					echo "<commentDate>";
					echo $date;
					echo "</commentDate>";
				echo "</comment>";
				
			}
			
			echo "</discussionList>";
			
		echo "</case>";
	
	}
	
	echo "</caseList>";

}


/*
 * SECTION GETCASEBYSEARCH 
 */
function getCaseBySearch($searchString){
	
	$now = time();
	$oneday = 86400;
	$range = $lastdays * $oneday;
	$lastdate = $now - $range;	
	
	require('../includes/gset.php');
	include ('../includes/database/connect.php');
	$sql_getCase = "SELECT cases.id_case, cases.editor, cases.last_modified, cases.topic, cases.status, staff.firstname, staff.lastname FROM cases, staff WHERE status > 4 AND cases.editor = staff.id_staff AND (topic like '%$searchString%' OR staff.lastname like '%$searchString%' OR staff.firstname like '%$searchString%') ORDER BY (last_modified) DESC;";	
	$getCase = mysql_query($sql_getCase);
	include ('../includes/database/closedb.php');
	
	// Writing XML Response Header
	header("Content-type: text/xml"); 
	echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
	echo "<caseList>";
	
	while ($case = mysql_fetch_object($getCase)){
		$idcase = $case->id_case;
		$lastname = $case->lastname;
		$firstname = $case->firstname;
		$topic = $case->topic;
		$status = $case->status;
		$editor = $case->editor;
		$lastmodified = $case->last_modified;
		
		$date = getDisplayDate($lastmodified);
		
		echo "<case>";
		echo "<caseId>";
		echo $idcase;
		echo "</caseId>";
		echo "<topic>";
		echo $topic;
		echo "</topic>";
		echo "<status>";
		echo $status;
		echo "</status>";
		echo "<editor>";
		echo $firstname." ".$lastname;
		echo "</editor>";
		echo "<caseEditorId>";
		echo $editor;
		echo "</caseEditorId>";
		echo "<lastModified>";
		echo $date;
		echo "</lastModified>";
		echo "</case>";
		
	}
	
	echo "</caseList>";
	
}


/*
 * SECTION GETCASEBYEDITOR
 */
function getCaseByEditor($edId){
	
	$now = time();
	$oneday = 86400;
	$range = $lastdays * $oneday;
	$lastdate = $now - $range;	
	
	require('../includes/gset.php');
	include ('../includes/database/connect.php');
	$sql_getCase = "SELECT cases.id_case, cases.editor, cases.last_modified, cases.topic, cases.status, staff.firstname, staff.lastname FROM cases, staff WHERE status > 4 AND cases.editor = staff.id_staff AND editor = $edId ORDER BY (last_modified) DESC;";	
	$getCase = mysql_query($sql_getCase);
	include ('../includes/database/closedb.php');
	
	// Writing XML Response Header
	header("Content-type: text/xml"); 
	echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
	echo "<caseList>";
	
	while ($case = mysql_fetch_object($getCase)){
		$idcase = $case->id_case;
		$lastname = $case->lastname;
		$firstname = $case->firstname;
		$topic = $case->topic;
		$status = $case->status;
		$lastmodified = $case->last_modified;
		
		$date = getDisplayDate($lastmodified);
		
		echo "<case>";
		echo "<caseId>";
		echo $idcase;
		echo "</caseId>";
		echo "<topic>";
		echo $topic;
		echo "</topic>";
		echo "<status>";
		echo $status;
		echo "</status>";
		echo "<editor>";
		echo $firstname." ".$lastname;
		echo "</editor>";
		echo "<lastModified>";
		echo $date;
		echo "</lastModified>";
		echo "</case>";
		
	}
	
	echo "</caseList>";
	
	
}


/*
 * FUCNTION GETDISPLAYDATE requires the blogdate integer and transforms the integer into a nls specific text.
 */
function getDisplayDate($blogdate) {
	
	require('../includes/gset.php');
	
	// Defining response language
	$session_language = $_SESSION['lang'];
	if ($session_language == "") {
	
		// Assuming Controller has not been triggered by a PHP Session
		if ($lang == 1)	{
			require ('../includes/language/german.php');	
		} elseif($lang == 2){
			require ('../includes/language/english.php');
		} elseif($lang == 3){
			require ('../includes/language/french.php');
		}
			
	}
	
	$aktuelle_zeit = time();
	$lessoneminute = $aktuelle_zeit - 60;
	$lessonehour = $aktuelle_zeit - 3600;
	$lessoneday = $aktuelle_zeit - 86400;
	$yesterday = $aktuelle_zeit - 172800;
	$week = $aktuelle_zeit - 604800;
	$twoweeks = $aktuelle_zeit - 1209600;
	$month = $aktuelle_zeit - 2419200;
	$twomonths = $aktuelle_zeit - 4838400;
	$halfyear = $aktuelle_zeit - 14515200;
	$year = $aktuelle_zeit - 29030400;	
					
	if ($blogdate > $lessoneminute){
	$display_blogdate = $nls_lessoneminute;
	} elseif ($blogdate > $lessonehour){
	$display_blogdate = $nls_lessonehour;
	} elseif ($blogdate > $lessoneday){
	$display_blogdate = $nls_lessoneday;
	} elseif ($blogdate > $yesterday) {
	$display_blogdate = $nls_yesterday;
	} elseif ($blogdate > $week){
	$display_blogdate = $nls_week;
	} elseif ($blogdate > $twoweeks){
	$display_blogdate = $nls_twoweeks;
	} elseif ($blogdate > $month){
	$display_blogdate = $nls_month;
	} elseif ($blogdate > $twomonths){
	$display_blogdate = $nls_twomonths;
	} elseif ($blogdate > $halfyear){
	$display_blogdate = $nls_halfyear;
	} elseif ($blogdate > $year){
	$display_blogdate = $nls_year;
	} else {
	$display_blogdate = $nls_older;
	}

	return $display_blogdate;
}

?>